package com.soso.base.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.soso.base.exception.unchecked.GeneralExceptionHandler;


@SuppressWarnings("serial")
public class BaseServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		this.doGet(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		if(action==null||action.equals("")){
			action="execute";
		}
		Class<? extends BaseServlet> clazz=this.getClass();
		try {
			Method method=clazz.getMethod(action, new Class[]{HttpServletRequest.class,HttpServletResponse.class});
			method.invoke(this, new Object[]{request,response});
		} catch (Exception e) {
			GeneralExceptionHandler.handle(e);
		}finally{
			response.flushBuffer();
			if(response.getWriter()!=null){
				response.getWriter().flush();
				response.getWriter().close();
			}
				
		}
		
	}
	
}
